import xlsxwriter as xw
import os
def xw_toExcel(data, filen):  # xlsxwriter库储存数据到excel
    fileName = filen+".xlsx"
    if(os.path.exists(fileName) == False):
        open(fileName,"wb")
        print("开始写入")
        workbook = xw.Workbook(fileName)  # 创建工作簿
        worksheet1 = workbook.add_worksheet("sheet1")  # 创建子表
        worksheet1.activate()  # 激活表
        title = ['省份', '学校','年份','录取最低分','批次线','批次']  # 设置表头
        worksheet1.write_row('A1', title)  # 从A1单元格开始写入表头
        i = 2  # 从第二行开始写入数据
        for j in range(len(data)):
            insertData = [data[j]["province"], data[j]["schoolname"],
                          data[j]["years"],data[j]["low"],
                          data[j]["batchline"],data[j]["batchname"]]
            row = 'A' + str(i)
            worksheet1.write_row(row, insertData)
            i += 1
        workbook.close()  # 关闭表
        print("写入完毕")
    else:
        print("文件已存在")